<template>
<!--
  <ContentWrap>
    &lt;!&ndash; 搜索工作栏 &ndash;&gt;
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="商品" prop="commodity">
        <el-input
          v-model="queryParams.commodity"
          placeholder="请输入商品"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="数量" prop="count">
        <el-input
          v-model="queryParams.count"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createAt">
        <el-input
          v-model="queryParams.createAt"
          placeholder="请输入创建时间"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="打款账号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入打款账号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="结款是否完成true/false" prop="pay">
        <el-select
          v-model="queryParams.pay"
          placeholder="请选择结款是否完成true/false"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="预留电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入预留电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="总计" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入总计"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['wms:sale:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['wms:sale:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
-->

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!--<el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="公司名称" align="center" prop="company" />
      <el-table-column label="打款账号" align="center" prop="number" />
      <el-table-column label="商品" align="center" prop="commodity" />
      <el-table-column label="数量" align="center" prop="count" />
      <el-table-column label="总计" align="center" prop="price" />
      <el-table-column label="预留电话" align="center" prop="phone" />
      <el-table-column label="备注" align="center" prop="description" />
      <el-table-column label="开票时间" align="center" prop="createAt" />
      <el-table-column label="更新时间" align="center" prop="updateAt" />
      <el-table-column label="结款是否完成" align="center" prop="pay">
        <template #default="scope">
          <el-tag v-if="!scope.row.pay" type="danger">等待结款</el-tag>
          <el-tag v-if="scope.row.pay" type="success">结款完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <!--<el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['wms:sale:update']"
          >
            编辑
          </el-button>-->
          <!--<el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['wms:sale:delete']"
          >
            删除
          </el-button>-->
          <span v-if="!scope.row.pay">
            <el-popconfirm
              confirm-button-text="确认"
              cancel-button-text="取消"
              :icon="InfoFilled"
              icon-color="#626AEF"
              title="确认结款？"
              @confirm="confirmSettle(scope.row.id)"
              @cancel="cancelSettle(scope.row.id)"
            >
            <template #reference>
              <el-button link type="primary" v-hasPermi="['wms:sale:settle']">结款</el-button>
            </template>
          </el-popconfirm>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SaleForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { SaleApi, SaleVO } from '@/api/wms/sale'
import SaleForm from './SaleForm.vue'
import { InfoFilled } from "@element-plus/icons-vue";

/** 销售 列表 */
defineOptions({ name: 'Sale' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SaleVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  commodity: undefined,
  company: undefined,
  count: undefined,
  createAt: undefined,
  description: undefined,
  number: undefined,
  pay: undefined,
  phone: undefined,
  price: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SaleApi.getSalePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: string) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SaleApi.deleteSale(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SaleApi.exportSale(queryParams)
    download.excel(data, '销售.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 确认结款按钮操作 */
const confirmSettle = async (id: string) => {
  try {
    await SaleApi.settleSale(id)
    message.success("确认结款成功！")
  } finally {
    // 刷新列表
    handleQuery()
  }
}

/** 取消结款按钮操作 */
const cancelSettle = async (id: string) => {
  console.log('cancelSettle!', id)
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
